﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Linq;
using BO = Viewer.BusinessObject.BusinessEntities;
using System.Web;

namespace Viewer.BusinessObject.BusinessEntities
{
    [Serializable()]
    public partial class QUNhomQuyenByToChuc : BusinessBase
    {
        public void Update()
        {

        }
        public string ToChuc { get; set; }
        public string TenUserOwner { get; set; }

        public List<QUNhomQuyenByToChuc> LoadDataByIdUser(int id)
        {
            var queryId = from a in this.Context.QUNhomQuyenByToChuc_NguoiDungs
                          where a.QUNguoiDung.NguoiDungID.Equals(id)
                          select a.NhomQuyenByToChucID;
            var queryTochuc = from a in this.Context.QUNhomQuyenByToChucs
                              where queryId.Contains(a.NhomQuyenByToChucID)
                              select a;
            return queryTochuc.ToList();

        }

        public QUNhomQuyenByToChuc GetById(int pNhomQuyenByToChucID)
        {
            QUNhomQuyenByToChuc nhomQuyen = this.Context.QUNhomQuyenByToChucs.Where(c => c.NhomQuyenByToChucID == pNhomQuyenByToChucID).FirstOrDefault();
            nhomQuyen.TenUserOwner = new QUNguoiDung().LoadDataById(nhomQuyen.UserIDOwner).TenDangNhap;
            return nhomQuyen;
        }

        public List<QUNhomQuyenByToChuc> GetAll()
        {
            List<QUNhomQuyenByToChuc> lst = this.Context.QUNhomQuyenByToChucs.ToList();
            foreach (QUNhomQuyenByToChuc  item in lst)
            {   
                item.ToChuc = GetToChucMoiNhom(item.NhomQuyenByToChucID);
                item.TenUserOwner = new BO.QUNguoiDung().LoadDataById(item.UserIDOwner).TenDangNhap;
            }
            return lst;
        }

        public List<QUNhomQuyenByToChuc> GetAllWithCurrentUserOwner()
        {
            List<QUNguoiDung> nguoiDung = new BO.QUNguoiDung().SearchNguoiDung(HttpContext.Current.User.Identity.Name);
            List<QUNhomQuyenByToChuc> lst = this.Context.QUNhomQuyenByToChucs.Where(c => c.UserIDOwner == nguoiDung[0].NguoiDungID).ToList();
            foreach (QUNhomQuyenByToChuc item in lst)
            {
                item.ToChuc = GetToChucMoiNhom(item.NhomQuyenByToChucID);
                item.TenUserOwner = new BO.QUNguoiDung().LoadDataById(item.UserIDOwner).TenDangNhap;
            }
            return lst;
        }

        public string GetToChucMoiNhom(int NhomQuyenByToChucID)
        {
            string result = string.Empty;
          
            List<NSToChuc> queryToChuc = (from a in this.Context.QUNhomQuyenByToChuc_ToChucs
                                          join b in this.Context.NSToChucs on a.ToChucID equals b.ToChucID
                                          where a.NhomQuyenByToChucID.Equals(NhomQuyenByToChucID)
                                          select b).ToList();
            foreach (NSToChuc item in queryToChuc)
            {
                if (result != string.Empty)
                {
                    result = string.Format("{0}, {1}", result, item.TenToChucFull);
                }
                else
                {
                    result = item.TenToChucFull;
                }
            }

            return result;
        }
        public void Save()
        {
            if (this.NhomQuyenByToChucID == 0)
            {
                //addNew
                this.Context.QUNhomQuyenByToChucs.InsertOnSubmit(this);
                this.Context.SubmitChanges();

            }
            else
            {

                //edit
                var oItem = this.GetById(this.NhomQuyenByToChucID);
                oItem.TenNhomQuyenByToChuc = this.TenNhomQuyenByToChuc;
                oItem.GhiChu = this.GhiChu;

                foreach (var item in oItem.QUNhomQuyenByToChuc_NguoiDungs)
                {
                    this.Context.QUNhomQuyenByToChuc_NguoiDungs.DeleteOnSubmit(item);
                }

                foreach (var item in oItem.QUNhomQuyenByToChuc_ToChucs)
                {
                    this.Context.QUNhomQuyenByToChuc_ToChucs.DeleteOnSubmit(item);
                }

                QUNhomQuyenByToChuc_NguoiDung oNguoiDung;

                foreach (var item in this.QUNhomQuyenByToChuc_NguoiDungs)
                {
                    oNguoiDung = new QUNhomQuyenByToChuc_NguoiDung();
                    oNguoiDung.NguoiDungID = item.NguoiDungID;
                    oNguoiDung.NhomQuyenByToChucID = item.NhomQuyenByToChucID;

                    oItem.QUNhomQuyenByToChuc_NguoiDungs.Add(oNguoiDung);
                }

                QUNhomQuyenByToChuc_ToChuc newItem2;
                foreach (var item in this.QUNhomQuyenByToChuc_ToChucs)
                {
                    newItem2 = new QUNhomQuyenByToChuc_ToChuc();
                    newItem2.NhomQuyenByToChucID = item.NhomQuyenByToChucID;
                    newItem2.ToChucID = item.ToChucID;
                    newItem2.IsView = item.IsView;
                    newItem2.IsAdd = item.IsAdd;
                    newItem2.IsEdit = item.IsEdit;
                    newItem2.IsDelete = item.IsDelete;
                    newItem2.IsToChucConThuaKe = item.IsToChucConThuaKe;

                    oItem.QUNhomQuyenByToChuc_ToChucs.Add(newItem2);
                }

                this.Context.SubmitChanges();
            }
        }

        public void Delete()
        {
            var oItem = this.GetById(this.NhomQuyenByToChucID);
            foreach (var item in oItem.QUNhomQuyenByToChuc_NguoiDungs)
            {
                this.Context.QUNhomQuyenByToChuc_NguoiDungs.DeleteOnSubmit(item);
            }

            foreach (var item in oItem.QUNhomQuyenByToChuc_ToChucs)
            {
                this.Context.QUNhomQuyenByToChuc_ToChucs.DeleteOnSubmit(item);
            }
            this.Context.QUNhomQuyenByToChucs.DeleteOnSubmit(oItem);
            this.Context.SubmitChanges();
        }

    }
}